package cn.lixin.product.controller;

import cn.lixin.basic.utils.AjaxResult;
import cn.lixin.basic.utils.LoginContext;
import cn.lixin.basic.utils.PageList;
import cn.lixin.org.domain.Employee;
import cn.lixin.org.mapper.EmployeeMapper;
import cn.lixin.product.domain.Product;
import cn.lixin.product.query.ProductQuery;
import cn.lixin.product.service.IProductService;
import cn.lixin.user.domain.Logininfo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/product")
public class ProductController {

    @Autowired
    private IProductService productService;
    @Autowired
    private EmployeeMapper employeeMapper;

    @PostMapping
    @ApiOperation(value = "高级查询",notes = "根据query高级查询")
    public PageList<Product> pageList(@RequestBody ProductQuery query, HttpServletRequest request){ // PathVariable：一个{}的时候可以不写
        return productService.queryPage(query);
    }

    @PostMapping("/onsale")
    @ApiOperation(value = "上架",notes = "上架")
    public AjaxResult onSale(@RequestBody List<Long> ids){
        try {
            productService.onSale(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("操作失败");
        }
    }

    @PostMapping("/offsale")
    @ApiOperation(value = "下架",notes = "下架")
    public AjaxResult offSale(@RequestBody List<Long> ids){
        try {
            productService.offSale(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("操作失败");
        }
    }

    /**
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Product queryById(@PathVariable("id") Long id){
        System.out.println(productService.queryById(id));
           return productService.queryById(id);
    }

    /**
     * 购买 ：
     * @param productId -
     * @param request 为了获取当前登录人 - 领养人
     * @return
     */
    @PostMapping("/buy/{id}")
    public AjaxResult buy(@PathVariable("id") Long productId, HttpServletRequest request){
        try {
            Long logininfoId = LoginContext.getCurrentLoginInfo(request).getId();
            productService.buy(productId,logininfoId);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("操作失败");
        }
    }
    /**
     * 修改添加
     * PutMapping: 修改或添加
     * @param product
     * @return
     */
    @PutMapping
    @ApiOperation(value = "服务添加或修改",notes = "如果有id就是修改否则就是添加")
    public AjaxResult addOrUpdate(@RequestBody Product product,HttpServletRequest request){
        try {
            Logininfo loginInfo = LoginContext.getCurrentLoginInfo(request);
            Employee employee = employeeMapper.loadBylogininfoId(loginInfo.getId());
            Long shop_id = employee.getShop_id();


            if (product.getId() == null){
                productService.save(product,shop_id);
            }else {
                productService.update(product);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("操作失败");
        }
    }

    /**
     * 删除
     * DeleteMapping: 删除
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除",notes = "")
    public AjaxResult del(@PathVariable("id") Long id){
        try {
            productService.remove(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("操作失败");
        }
    }


    /**
     * 删除所有
     *
     * @param ids
     * RequestBody : 数组也是对象
     */
    @PatchMapping
    @ApiOperation(value = "删除",notes = "如果有id就是修改否则就是添加")
    public AjaxResult removeList(@RequestBody List<Long> ids){
        try {
            productService.removeList(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("操作失败");
        }
    }



}
